package com.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.example.apple.ddhr_new.R;
import com.example.lzbase.BaseActivity;
import com.shizhefei.view.largeimage.LargeImageView;
import com.shizhefei.view.largeimage.factory.InputStreamBitmapDecoderFactory;

import java.io.IOException;

import butterknife.BindView;

public class ImageActivity extends BaseActivity {

    private float height;
    private float width;
    private String image;
    private String name;

    @Override
    protected int titleText() {
        return R.string.app_name;
    }
    @BindView(R.id.iv_icon)
    LargeImageView ivIcon;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {



        width= getIntent().getExtras().getFloat("width");

        height = getIntent().getExtras().getFloat("height");
        image = getIntent().getExtras().getString("image");
        name = getIntent().getExtras().getString("name");
        setTitleText(name);
        LargeImageView imageView = ivIcon;
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        params.height = (int) (d.getWidth() * (height / width));
        imageView.setLayoutParams(params);
        try {
            ivIcon.setImage(new InputStreamBitmapDecoderFactory(getResources().getAssets().open(image)) );
        } catch (IOException e) {

            Log.e("sdada",e.toString());
            e.printStackTrace();


        }
//
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image;
    }
    public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            inSampleSize *= 2;
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
}
